/* ************************************************************************ */
/*																			*/
/*  haXe Video 																*/
/*  Copyright (c)2008 Russell Weir, Nicolas Cannasse						*/
/*																			*/
/* This library is free software; you can redistribute it and/or			*/
/* modify it under the terms of the GNU Lesser General Public				*/
/* License as published by the Free Software Foundation; either				*/
/* version 2.1 of the License, or (at your option) any later version.		*/
/*																			*/
/* This library is distributed in the hope that it will be useful,			*/
/* but WITHOUT ANY WARRANTY; without even the implied warranty of			*/
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU		*/
/* Lesser General Public License or the LICENSE file for more details.		*/
/*																			*/
/* ************************************************************************ */
package samples;

import flash.net.NetConnection;
import flash.net.SharedObject;
import flash.net.Responder;

class Objects extends Display {
	static var name			: String = "testobj";
	static var persistent	: Bool = true;
	var nc		: NetConnection;
	var resp	: Responder;
	var so		: SharedObject;
	var host	: String;
	var count 	: Int;
	var bpos : Float;

	public function new(host) {
		bpos = 25;
		flash.net.SharedObject.defaultObjectEncoding = 0;
		super();
		this.host = host;
		trace("Connecting to "+host);

		nc = new NetConnection();
		nc.addEventListener(flash.events.NetStatusEvent.NET_STATUS,onNetStatusEvent);

		trace("connecting");
		nc.connect(host+"/SOSample");
// 		resp = new flash.net.Responder(onResult, onStatus);
//		nc.call("russ/add", resp, 1, 2);
		// required
		count = 0;

	}

	function addButton( text, onClick ) {
		var t = new flash.text.TextField();
		t.defaultTextFormat = Main.getTextFormat();
		t.text = text;
		t.width = t.textWidth + 6;
		t.height = 18;
		t.selectable = false;
		t.x = 2;

		var b = new flash.display.MovieClip();
		b.graphics.beginFill(0xFFEEDD);
		b.graphics.lineStyle(2,0x000000);
		b.graphics.drawRect(0,0,t.width,18);
		b.addChild(t);

		var sb = new flash.display.SimpleButton();
		sb.upState = b;
		sb.overState = b;
		sb.downState = b;
		sb.hitTestState = b;
		sb.useHandCursor = true;
		sb.addEventListener(flash.events.MouseEvent.CLICK,callback(doClick,onClick));
		flash.Lib.current.addChild(sb);

		sb.y = bpos;
		sb.x = 2;
		bpos += t.height + 5;
	}

	static function doClick( onClick, e ) {
		try {
			onClick();
		} catch( e : Dynamic ) {
			Main.doTrace(e);
		}
	}

	function doConnect() {
		so = SharedObject.getRemote(Objects.name, nc.uri, persistent,false);
		so.addEventListener(flash.events.SyncEvent.SYNC, onSync);
		so.addEventListener(flash.events.AsyncErrorEvent.ASYNC_ERROR, onSyncError);
		so.addEventListener(flash.events.NetStatusEvent.NET_STATUS,onSONetStatusEvent);
		so.connect(nc); // params?
		so.setProperty("key","value");
	}

	public function onNetStatusEvent(e : Dynamic) : Void {
		trace(here.methodName);
		trace(e.info);
		if( e.info.code == "NetConnection.Connect.Success" ) {
			resp = new Responder(onResult, onStatus);
			addButton("Connect",doConnect);
			//nc.call("add", resp, 400, 234);
		}
	}

	function onSONetStatusEvent(e:flash.events.NetStatusEvent) {
		trace(here.methodName + " " + e);
	}

	function onSync(e:flash.events.SyncEvent) {
		for(i in e.changeList) {
			if(untyped i.code == "change") {
				onSyncChange(Std.string(untyped i.name), untyped i.oldValue);
			}
			else if(untyped i.code == "reject") {
				onSyncReject(Std.string(untyped i.name), untyped i.oldValue);
			}
			else if(untyped i.code == "success") {
				onSyncSuccess(Std.string(untyped i.name));
			}
		}
	}

	function onSyncError(e:flash.events.AsyncErrorEvent) {
		trace(here.methodName + " " + e);
	}

	function onSyncChange(name:String, oldVal:Dynamic) {
		trace(name+" changed");
	}
	function onSyncReject(name:String, oldVal:Dynamic) {
		trace("Change to "+name+" rejected");
	}
	function onSyncSuccess(name:String) {
		trace("Change to "+name+" accepted");
	}

	public function doStop() {
		nc.close();
	}

	function onResult(obj) : Void {
		trace(here.methodName);
		trace("The result is " + obj);
	}

	function onStatus(obj) : Void {
		trace(here.methodName);
		trace("The status is " + obj);
	}
}